<?php

namespace App\Http\Middleware;

use Closure;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Cache;

class UserToken
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        // if (gettype(JWTAuth::parseToken()) == 'object' && JWTAuth::parseToken()->check()) {
        //     $guard = JWTAuth::parseToken()->getPayload()['role'];
        //     if ($guard == 'user') {
        //         $response = $next($request);
        //         return $response;
        //     }
        // }
        $token = $request->header('token');
        if (Cache::get($token)) {
            $response = $next($request);
            return $response;
        }

        return error('无访问权限', 401);
    }
}
